package com.foreknow.dao.impl;

import com.foreknow.dao.OrderDetailetDao;
import com.foreknow.dao.OrdersDao;
import com.foreknow.po.*;
import com.foreknow.util.CommonUtil;
import com.foreknow.util.DBUtil;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection conn = null;
    private PreparedStatement pst = null;

    @Override
    public Integer saveOrders(String userId, Integer businessId, Integer daId, Double orderTotal) throws SQLException {
        String sql = "insert into orders(userId,businessId,daId,orderTotal,orderDate) values(?,?,?,?,?)";
        Integer ordersId = 0;
        try{
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,daId);
            pst.setDouble(4,orderTotal);
            pst.setString(5, CommonUtil.getCurrentDate());
            pst.executeUpdate();
            ResultSet rs = pst.getGeneratedKeys();
            while (rs.next()){
                ordersId = rs.getInt(1);
            }
        } finally {
            DBUtil.close(pst);
        }
        return ordersId;
    }


//    @Override
//    public Integer saveToOrdersDetailet(Integer ordersId, List<OrderDetailet> list) throws SQLException {
//        //String sql = "insert into orderdetailet(orderId,foodId,quantity) value ";
//        StringBuffer sql = new StringBuffer("insert into orderdetailet(orderId,foodId,quantity) value ");
//        Integer result = 0 ;
//        try {
//            conn = DBUtil.getConnection();
//            for (int i=0;i<list.size();i++){
//                sql.append("(" + ordersId +","+list.get(i).getFoodId()+","+list.get(i).getQuantity()+"),");
//            }
//            sql.deleteCharAt(sql.length()-1);
//            pst = conn.prepareStatement(sql.toString());
//            result = pst.executeUpdate();
//        }finally {
//                DBUtil.close(pst);
//        }
//        return result;
//    }


    @Override
    public Orders getOrdersById(Integer orderId) throws SQLException {
        String sql = "select  orders.orderId,userId,orderDate,orders.businessId,businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,remarks,orderTotal,orderState,daId  " +
                "from orders " +
                "join business on business.businessId = orders.businessId " +
                "where orders.orderId = ?";
        Orders orders = new Orders();
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1,orderId);
            ResultSet rs = pst.executeQuery();
            List<Orders> list  = new ArrayList<>();
            while(rs.next()){
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
            }
        } finally {
            DBUtil.close(pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws SQLException {
        String sql ="select orders.orderId,userId,orderDate,orders.businessId,businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,remarks,orderTotal,daId,orderState " +
                " from orders join business  " +
                " on business.businessId = orders.businessId " +
                " where orders.userId = ? ";
        List<Orders> list = new ArrayList<>();

        try{
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setString(1,userId);
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
                list.add(orders);
            }
        } finally {
            DBUtil.close(pst);
        }


        return list;
    }




    public static void main(String[] args) {
        OrdersDao dao = new OrdersDaoImpl();
        try {
//            Integer result = dao.saveOrders("12345671111",10001,1,100.00);
//            System.out.println(result);
//            Integer result = dao.saveToOrdersDetailet(11,list);
//            System.out.println(result);
//            Orders orders = dao.getOrdersById(6);
//            System.out.println(orders.getOrderDate()+"--"+orders.getBusiness().getBusinessName());
//            List<OrderDetailet> list = orders.getList();
//            System.out.println(list.size());
//            StringBuffer sql = new StringBuffer("insert into orderdetailet(orderId,foodId,quantity) value(),,,");
//            sql.deleteCharAt(sql.lastIndexOf(","));
//            System.out.println(sql.toString());
//            List<OrderDetailet> list = new ArrayList<>();
//            OrderDetailet orderDetailet = new OrderDetailet();
//            orderDetailet.setQuantity(5);
//            orderDetailet.setFoodId(1);
//            orderDetailet.setOrderId(10);
//            list.add(orderDetailet);
//            int result = 0;

                List<Orders> list = dao.listOrdersByUserId("12345671111");
                for (Orders o : list){
                    List<OrderDetailet> listod = o.getList();
                    System.out.println(o.getBusiness().getBusinessName());

                    System.out.println("-----");
                    for (OrderDetailet od:listod){
                        System.out.println(od.getFoodId());
                        System.out.println("shuliang"+od.getQuantity());
                    }
                    System.out.println("next..............");
                }

                Orders orders = dao.getOrdersById(6);
            System.out.println(orders.getBusiness().getBusinessName());


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
        }
}
